package com.gmail.hongyz.myfuse.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gmail.hongyz.myfuse.dataengine.JsonRequest;
import com.gmail.hongyz.myfuse.dataengine.JsonRequestImpl;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class RequestUtils {

	private static final Log log = LogFactory.getLog(RequestUtils.class);
	
	public static JSONObject convertToJson(HttpServletRequest request) throws IOException {
		String jsonData = StringUtils.slurp(request.getInputStream());
		JSONObject json = JSONObject.fromObject(jsonData);
		return json;
	}
	
	public static JsonRequest convertToJsonRequest(HttpServletRequest request) throws IOException {
		String jsonData = StringUtils.slurp(request.getInputStream());
		log.debug("jsonData: " + jsonData);
		JSONObject json = null;
		JsonRequestImpl jsonRequest = new JsonRequestImpl();
		jsonRequest.setServletRequest(request);
		try {
			json = JSONObject.fromObject(jsonData);
		} catch (JSONException e) {
			// TODO: handle exception
			json = new JSONObject();
		}
		jsonRequest.setJson(json);
//		JSONObject jsonParameters = (JSONObject) json.get("parameters");
		return jsonRequest;
	}
}
